/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.ReconDAO;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.dao.impl.ReconDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Invoice;
import com.ivanbiz.model.Jurnal;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Pembayaran;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

/**
 *
 * @author IW20149X
 */
public class ReconsileService extends SelectorComposer<Component> {

    KelasDAO kelasDAO;
    ReconDAO reconDAO;
    SimpleDateFormat sdf;
    NumberFormat numberFormat;
    @Wire
    Combobox listKelas;
    @Wire
    Listbox listDaftarKelas;
    @Wire
    Listbox listInvoice;
    @Wire
    Listbox listPembayaran;
    @Wire
    Listbox listJurnal;
    @Wire
    Textbox jumlahPeserta;
    @Wire
    Textbox totalTagihan;
    @Wire
    Textbox totalPembayaran;
    Map map;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        kelasDAO = new KelasDAOImpl();
        List<Kelas> loadKelas = kelasDAO.getDataByEquals(Kelas.class, "statusKelas", "N");
        for (Kelas kelas : loadKelas) {
            Comboitem item = new Comboitem();
            item.setValue(kelas);
            item.setLabel(kelas.getTransactionReference());
            listKelas.appendChild(item);
        }
    }

    @Listen("onClick=#lihat;")
    public void lihat() throws Exception {
        sdf = new SimpleDateFormat("dd-MMMM-yyyy");
        numberFormat = NumberFormat.getNumberInstance();
        reconDAO = new ReconDAOImpl();
        Kelas kelas = listKelas.getSelectedItem().getValue();
        map = reconDAO.getDataByKelas(kelas.getId());
        updateTableDaftarKelas();
        updateTableInvoice();
        updateTablePembayaran();
        updateTableJurnal();
    }

    private void updateTableDaftarKelas() {
        List<DaftarKelas> loadDaftarKelas = (List<DaftarKelas>) map.get("listDaftarKelas");
        int no = 0;
        int x = 0;
        for (DaftarKelas daftarKelas : loadDaftarKelas) {
            no += 1;
            Listitem item = new Listitem();
            item.setValue(daftarKelas);
            item.appendChild(new Listcell(String.valueOf(no)));
            item.appendChild(new Listcell(daftarKelas.getMurid().getNama()));
            item.appendChild(new Listcell(daftarKelas.getMurid().getBank().getNama()));
            item.appendChild(new Listcell(daftarKelas.getMurid().getTelp() + "/" + daftarKelas.getMurid().getHandphone()));
            item.appendChild(new Listcell(null == daftarKelas.getStatus() ? "N" : "Y"));
            listDaftarKelas.appendChild(item);
            ++x;
        }
        jumlahPeserta.setValue(String.valueOf(loadDaftarKelas.size()));
    }

    private void updateTableInvoice() {
        List<Invoice> loadInvoice = (List<Invoice>) map.get("listInvoice");
        int no = 0;
        int x = 0;
        double total = 0;
        for (Invoice invoice : loadInvoice) {
            no += 1;
            Listitem item = new Listitem();
            item.setValue(invoice);
            item.appendChild(new Listcell(String.valueOf(no)));
            item.appendChild(new Listcell(sdf.format(invoice.getDate())));
            item.appendChild(new Listcell(invoice.getNII()));
            item.appendChild(new Listcell(invoice.getBank().getNama()));
            item.appendChild(new Listcell(numberFormat.format(invoice.getJumlahTagihan())));
            item.appendChild(new Listcell(invoice.getStatus().equals("0") ? "N" : "Y"));
            listInvoice.appendChild(item);
            total += invoice.getJumlahTagihan();
            ++x;
        }
        totalTagihan.setValue("Rp " + numberFormat.format(total) + ",00");
    }

    private void updateTablePembayaran() {
        List<Pembayaran> loadPembayaran = (List<Pembayaran>) map.get("listPembayaran");
        int no = 0;
        int x = 0;
        double total = 0;
        for (Pembayaran pembayaran : loadPembayaran) {
            no += 1;
            Listitem item = new Listitem();
            item.setValue(pembayaran);
            item.appendChild(new Listcell(String.valueOf(no)));
            item.appendChild(new Listcell(sdf.format(pembayaran.getDatePosting())));
            item.appendChild(new Listcell(pembayaran.getInvoice().getNII()));
            item.appendChild(new Listcell(numberFormat.format(pembayaran.getJumlah())));
            item.appendChild(new Listcell(pembayaran.getStatus().equals("0") ? "N" : "Y"));
            listPembayaran.appendChild(item);
            total += pembayaran.getJumlah();
            ++x;
        }
        totalPembayaran.setValue("Rp " + numberFormat.format(total) + ",00");


    }

    private void updateTableJurnal() {
        List<Jurnal> loadJurnal = (List<Jurnal>) map.get("listJurnal");
        int no = 0;
        int x = 0;
        String kode = "";
        for (Jurnal jurnal : loadJurnal) {
            Listitem item = new Listitem();
            item.setValue(jurnal);
            if (!jurnal.getTransactionReference().equals(kode)) {
                no += 1;
                kode = jurnal.getTransactionReference();
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(sdf.format(jurnal.getDateReference())));
                item.appendChild(new Listcell(kode));
                item.appendChild(new Listcell(jurnal.getAccountingReference()));
                item.appendChild(new Listcell(jurnal.getCurrency()));
            } else {
                item.appendChild(new Listcell(""));
                item.appendChild(new Listcell(""));
                item.appendChild(new Listcell(""));
                item.appendChild(new Listcell(""));
                item.appendChild(new Listcell(""));
            }
            item.appendChild(new Listcell(numberFormat.format(jurnal.getDebit())));
            item.appendChild(new Listcell(numberFormat.format(jurnal.getCredit())));
            item.appendChild(new Listcell(jurnal.getGLAccount()));
            listJurnal.appendChild(item);
            ++x;
        }
    }
}
